package com.zhangtai.modules.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.CarouselDao;
import com.zhangtai.modules.dao.RelativeRecordsDao;
import com.zhangtai.modules.dto.JobDto;
import com.zhangtai.modules.dto.NoticeListVo;
import com.zhangtai.modules.entity.common.NoticeEntity;
import com.zhangtai.modules.entity.system.CarouselEntity;
import com.zhangtai.modules.service.baseFromMssql.BaseMssqlServiceImpl;
import com.zhangtai.modules.service.common.NoticeService;
import com.zhangtai.modules.service.system.AnnouncementService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags="员工自助主页信息接口")
@RequestMapping("member")
@Slf4j
public class HomeMemberController {

    @Autowired
    private CarouselDao carouselDao;
    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private MemberLoginController memberLoginController;

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private RelativeRecordsDao relativeRecordsDao;
    @Autowired
    private BaseMssqlServiceImpl jobService;

    @PostMapping("getCarouselList")
    @ApiOperation(value="查询轮播图")
//    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R getCarouselList() {
        List<CarouselEntity> carouselEntityList = carouselDao.selectList(new QueryWrapper<CarouselEntity>()
                .eq("is_delete", 0)
                .eq("is_show", 1)
                .orderByDesc("order_num")
                .orderByDesc("create_time"));
        return R.ok(carouselEntityList);
    }

    @GetMapping("/getAnnouncementPage")
    @ApiOperation(value="分页查询公告")
//    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R getAnnouncementPage(@RequestParam( value = "type" ,defaultValue = "1") Integer type,
                                 @RequestParam( value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                 @RequestParam( value = "pageSize" ,defaultValue = "10") Integer pageSize) {

        PageResult  pageResult= announcementService.queryPage(null,null,null,type,pageNum,pageSize);
        return R.ok(pageResult);
    }

    @PostMapping("/getNotice")
    @ApiOperation(value="条件分页查询通知")
//    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R<PageResult<NoticeEntity>> getNoticeList(@RequestBody @Valid NoticeListVo vo, HttpServletRequest request)throws  Exception{
        R r =memberLoginController.getUserInfo(request);

        if(!R.isOk.equals(r.getStatus())){
            return r;
        }
        JSONObject user = (JSONObject) r.getData();
        String accepterCode = user.getString("usercode");
        PageResult<NoticeEntity> pageResult = noticeService.queryPage(vo.getType(),vo.getMsgType(),
                vo.getNoticeType(),accepterCode,vo.getTitle(),vo.getIsRead(),vo.getIsDone(), vo.getPageNum(), vo.getPageSize());
        return R.ok(pageResult);
    }

    @GetMapping("/findJobByOrgId/{orgId}")
    @ApiOperation(value="根据部门id查找职位")
    public R<List<JobDto>> findJobByOrgId(@PathVariable("orgId") Long orgId){
        List<JobDto> jobEntities = jobService.findJobByKeyword(orgId);
        return R.ok(jobEntities);

    }



}
